package com.besttone.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CloudSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;

/**
 * Created by zhengzhenzhen on 2017/4/20.
 */
public class SolrCommonUtil {
    private static Log _log = LogFactory.getLog(SolrCommonUtil.class);
    private static CloudSolrServer cloudSolrServer;
    /**
     * 获取CloudSolrServer
     *
     * @param zkHost
     * @return
     */
    private static synchronized CloudSolrServer getCloudSolrServer(
            final String zkHost) {
        if (cloudSolrServer == null) {
            try {
                _log.info("创建cloudSolrServer");
                cloudSolrServer = new CloudSolrServer(zkHost);
            } catch (Exception e) {
                _log.info("cloudSolrServer创建异常:" + e);
            }
        }
        return cloudSolrServer;
    }
    /**
     * 连接CloudSolrServer
     */
    private static void connectCloudSolrServer(String defaultConnection) {
        cloudSolrServer = getCloudSolrServer(PropertieUtils
                .getProperty("zkHost"));
        _log.info("The Cloud SolrServer Instance has benn created!");
        cloudSolrServer.setDefaultCollection(PropertieUtils
                .getProperty(defaultConnection));
        cloudSolrServer.setZkClientTimeout(Integer.valueOf(PropertieUtils
                .getProperty("zkClientTimeout")));
        cloudSolrServer.setZkConnectTimeout(Integer.valueOf(PropertieUtils
                .getProperty("zkConnectTimeout")));
        try{
            cloudSolrServer.connect();
        }catch(Exception e){
            _log.info("cloundSolrServer connection exception:"+e);
        }
        _log.info("The cloud Server has been connected !!!!");
    }
    /**
     * 查询详情索引
     *
     * @param querySql
     * @param currentPage
     * @param perPageSize
     * @return
     */
    public static SolrDocumentList search(String collectionName,String querySql,String fields,String sortField,SolrQuery.ORDER order,Integer currentPage,
                                          Integer perPageSize) {

        SolrQuery query = new SolrQuery();
        query.setQuery(querySql);
        System.out.println("querySql:"+querySql);
        query.setStart((currentPage - 1) * perPageSize);
        query.setRows(perPageSize);
        query.setFields(fields);
        query.setSort(sortField, order);
        try {
            connectCloudSolrServer(collectionName);
            cloudSolrServer.setDefaultCollection(collectionName);
            QueryResponse response = cloudSolrServer.query(query);
            SolrDocumentList docs = response.getResults();
            _log.info("文档个数：" + docs.getNumFound());
            _log.info("查询时间：" + response.getQTime());
            return docs;
        } catch (SolrServerException e) {
            _log.info("SolrServer异常：" + e);
        } catch (Exception e) {
            _log.info("Solr查询异常：" + e);
        }
        return null;
    }
}
